package main

import (
	"fmt"
	"net/http"
)

// main
//
//	@Description: Cookie & Session
func main() {

	http.HandleFunc("/cookie/set1", setCookie1)
	http.HandleFunc("/cookie/set2", setCookie2)
	http.HandleFunc("/cookie/get1", getCookie1)

}

func setCookie1(w http.ResponseWriter, r *http.Request) {
	c1 := http.Cookie{
		Name:     "a",
		Value:    "a-value",
		HttpOnly: true,
	}
	c2 := http.Cookie{
		Name:     "b",
		Value:    "b-value",
		HttpOnly: true,
	}
	//将 Cookie 发送给浏览器,即添加第一个 Cookie
	w.Header().Set("Set-Cookie", c1.String())
	//再添加一个 Cookie
	w.Header().Add("Set-Cookie", c2.String())
}

func setCookie2(w http.ResponseWriter, r *http.Request) {
	c1 := http.Cookie{
		Name:     "c-2",
		Value:    "c-value-2",
		HttpOnly: true,
		MaxAge:   60,
	}
	c2 := http.Cookie{
		Name:     "d-2",
		Value:    "c-value-2",
		HttpOnly: true,
		MaxAge:   60,
	}
	http.SetCookie(w, &c1)
	http.SetCookie(w, &c2)
}

func getCookie1(w http.ResponseWriter, r *http.Request) {
	//获取请求头中的 Cookie
	cookies := r.Header["Cookie"]
	fmt.Fprintln(w, cookies)
}

//而Go标准库中没有内置的Session管理，所以需要自己实现或使用第三方库。
